package com.fw.system.admin.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.common.IdXD;
import com.fw.system.admin.domain.TbMoneys;
import com.fw.system.admin.service.ITbMoneysService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.fw.system.admin.mapper.TbAmountLogMapper;
import com.fw.system.admin.domain.TbAmountLog;
import com.fw.system.admin.service.ITbAmountLogService;
import com.fw.utils.DateUtils;

/**
 * 支出收益Service业务层处理
 * 
 * @author xxx
 * @date 2021-12-03
 */
@Service
public class TbAmountLogServiceImpl extends ServiceImpl<TbAmountLogMapper, TbAmountLog> implements ITbAmountLogService
{
    @Autowired
    private TbAmountLogMapper tbAmountLogMapper;

    @Autowired
    private IdXD idXD;

    /**
     * 查询支出收益
     * 
     * @param id 支出收益ID
     * @return 支出收益
     */
    @Override
    public TbAmountLog selectTbAmountLogById(String id)
    {
        return tbAmountLogMapper.selectTbAmountLogById(id);
    }

    /**
     * 查询支出收益列表
     * 
     * @param tbAmountLog 支出收益
     * @return 支出收益
     */
    @Override
    public List<TbAmountLog> selectTbAmountLogList(TbAmountLog tbAmountLog)
    {
        return tbAmountLogMapper.selectTbAmountLogList(tbAmountLog);
    }

    /**
     * 新增支出收益
     * 
     * @param tbAmountLog 支出收益
     * @return 结果
     */
    @Override
    public int insertTbAmountLog(TbAmountLog tbAmountLog)
    {
        tbAmountLog.setCreateTime(DateUtils.getNowDate());
        tbAmountLog.setId(idXD.nextId());
        return tbAmountLogMapper.insertTbAmountLog(tbAmountLog);
    }

    /**
     * 修改支出收益
     * 
     * @param tbAmountLog 支出收益
     * @return 结果
     */
    @Autowired
    private ITbMoneysService moneysService;
    @Override
    public int updateTbAmountLog(TbAmountLog tbAmountLog)
    {
        // 判断吧
        // 如果 logType == 1 || == 2  充值，提现
        TbAmountLog tbAmountLog1 = tbAmountLogMapper.selectById(tbAmountLog.getId());
        if (tbAmountLog.getLogType().equals(1L) || tbAmountLog.getLogType().equals(2L)) {
            // 开始操作
            if (!tbAmountLog1.getIsStatus().equals(tbAmountLog.getIsStatus())){
                // 状态变化
                // 如果是 充值,成功就 钱包 +   失败不管
                // 如果 是提现,成功就不管, 失败就 +
                if (tbAmountLog.getLogType().equals(1L) && tbAmountLog.getIsStatus().equals(3L)){
                    TbMoneys tbMoneys = moneysService.getOne(Wrappers.<TbMoneys>lambdaQuery().eq(TbMoneys::getUserId, tbAmountLog.getUserId()));
                    tbMoneys.setAmount(tbMoneys.getAmount().add(tbAmountLog.getLogCount()));
                    moneysService.updateById(tbMoneys);
                }
                if (tbAmountLog.getLogType().equals(2L) && tbAmountLog.getIsStatus().equals(2L)){
                    TbMoneys tbMoneys = moneysService.getOne(Wrappers.<TbMoneys>lambdaQuery().eq(TbMoneys::getUserId, tbAmountLog.getUserId()));
                    tbMoneys.setAmount(tbMoneys.getAmount().add(tbAmountLog.getLogCount()));
                    moneysService.updateById(tbMoneys);
                }
            }

        }
        return tbAmountLogMapper.updateTbAmountLog(tbAmountLog);
    }

    /**
     * 批量删除支出收益
     * 
     * @param ids 需要删除的支出收益ID
     * @return 结果
     */
    @Override
    public int deleteTbAmountLogByIds(String[] ids)
    {
        return tbAmountLogMapper.deleteTbAmountLogByIds(ids);
    }

    /**
     * 删除支出收益信息
     * 
     * @param id 支出收益ID
     * @return 结果
     */
    @Override
    public int deleteTbAmountLogById(String id)
    {
        return tbAmountLogMapper.deleteTbAmountLogById(id);
    }
}
